#include "notify_typing_task.h"

#include "base/logging.h"
#include "im/adapters/session_adapter.h"
#include "im/adapters/message_outlet_adapter.h"

namespace imserver {

const char * MessageTypeName(int type);

void NotifyTypingTask::run() {
  SessionList sessions;
  SessionAdapter::Instance().GetSessionList(receiver_, &sessions, 2);

  ImMessage typing_message;
  typing_message.sender = sender_;
  typing_message.receiver = receiver_;
  typing_message.timestamp = time(NULL);
  SetMessageType(typing_message, MSG_TYPE_TYPING);
  typing_message.body = MessageTypeName(MSG_TYPE_TYPING);

  for(size_t i = 0; i < sessions.size(); ++i) {
    int res = MessageOutletAdapter::Instance().DistributeMessage(sessions[i].key, typing_message, 2);
    if (res == 0) {
      LOG_INFO("MessageOutletAdapter send typing message ok.");
    } else {
      LOG_WARN("MessageOutletAdapter send typing message err.");
    }
  }
}

}


